/*
 * Date：10/23/18 10:08 PM
 * Author: guomw
 *
 */

package org.elanfox.intellicommunity.biz.repository.equipment;

import org.elanfox.intellicommunity.biz.entity.equipment.CardPO;
import org.elanfox.intellicommunity.data.base.base.JpaSearchableRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 有源卡Repository
 *
 * @author loutianxi
 * @date 2019-12-04
 */
@Repository
public interface CardRepository extends JpaSearchableRepository<CardPO, Integer> {

    /**
     * 更新启用状态
     *
     * @param cardId
     * @param enable
     * @return
     */
    @Query("update CardPO c set c.enable=?2 where c.cardId=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    int updateEnableById(Integer cardId, boolean enable);

    /**
     * 更新电压状态
     *
     * @param cardId
     * @param enable
     * @return
     */
    @Query("update CardPO c set c.voltageStatus=?2 where c.cardNo=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    void updateVoltageStatusById(Long cardNo, int voltageStatus);


    /**
     * 注销卡
     *
     * @param cardId
     * @return
     */
    @Query("update CardPO c set c.deleteStatus=1 where c.cardId=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    int updateDeleteStatus(Integer cardId);

    /**
     * 解绑
     *
     * @param cardNo
     * @return
     */
    @Query("update CardPO c set c.user=null where c.cardNo=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    int unbinding(Long cardNo);


    /**
     * 根据cardNo查询卡po
     *
     * @param cardNo
     * @return
     */
    CardPO findByCardNo(Long cardNo);

    List<CardPO> findByDeleteStatusIsFalse();
}
